package com.hangwei.paperless.adapter;

import android.os.Build;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.Html;
import android.text.Spanned;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.hangwei.paperless.R;
import com.hangwei.paperless.model.bean.PartsRoutingSheet;
import com.hangwei.paperless.widget.URLImageParser;
import com.twelve.all.adapter.MyAdapter;
import com.twelve.all.utils.Tools;

import org.xml.sax.XMLReader;

import java.util.List;

public class CardStepAdapter extends MyAdapter<PartsRoutingSheet.StepListBean, BaseViewHolder> {
    public CardStepAdapter( @Nullable List<PartsRoutingSheet.StepListBean> data) {
        super(R.layout.item_card_step, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, PartsRoutingSheet.StepListBean item) {
        String source = Tools.checkedString(item.description);
        //替换掉P标签开头
        String all = source.replaceAll("<p>", "").replaceAll("<p .?>", "");
        while (all.endsWith("</p>")) {
            all = all.substring(0, all.lastIndexOf("</p>"));
        }
        while (all.endsWith("<br/>")) {
            all = all.substring(0, all.lastIndexOf("<br/>"));
        }
        all = all.replaceAll("</p>", "<br/>");
        Spanned html;
        TextView tvDes = helper.getView(R.id.tvDes);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N)
            html = Html.fromHtml(all, Html.FROM_HTML_MODE_COMPACT, new URLImageParser(tvDes, mContext), new Html.TagHandler() {
                @Override
                public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {

                }
            });
        else
            html = Html.fromHtml(all, new URLImageParser(tvDes, mContext), new Html.TagHandler() {
                @Override
                public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {

                }
            });
        helper.setText(R.id.tvSeq, Tools.checkedString(item.seq))
                .setText(R.id.tvName,Tools.checkedString(item.item))
                .setText(R.id.tvDes, html);
    }
}
